import {
	connect
} from 'react-redux';
import {
	VisibilityFilters,
	toggleTodo,
	deleteTodo
} from '../actions';
import TodoList from '../components/TodoList';

const getVisibleTodos = (todos, filter) => {
	switch (filter) {
		case VisibilityFilters.SHOW_ALL:
			return todos
		case VisibilityFilters.SHOW_ACTIVE:
			return todos.filter(todo => !todo.completed)
		case VisibilityFilters.SHOW_COMPLETED:
			return todos.filter(todo => todo.completed)
		default:
			throw new Error('Unknown filter: ' + filter)
	}
}

const mapStateToProps = (state, ownProps) => ({
	todos: getVisibleTodos(state.todos, state.visibilityFilter)
})

const mapDispatchToProps = (dispatch, ownProps) => ({
	onToggle: (id) => dispatch(toggleTodo(id)),
	onDelete: (id) => dispatch(deleteTodo(id))
})

export default connect(mapStateToProps, mapDispatchToProps)(TodoList)